package com.link2room.ironhide.facade.impl;


import com.link2room.common.constant.BaseConstant;
import com.link2room.common.dto.result.base.HotelBaseInfoDto;
import com.link2room.common.entity.BaseEntity;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.redis.RedisUtil;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.CommonUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.enums.exception.IronhideSysExceptionEnum;
import com.link2room.ironhide.dto.param.manager.HotelsManagerParamDto;
import com.link2room.ironhide.dto.result.base.L2RHotelPictureDto;
import com.link2room.ironhide.dto.result.manager.HotelInfoDto;
import com.link2room.ironhide.entity.L2RHotelBaseinfo;
import com.link2room.ironhide.entity.L2RHotelPicture;
import com.link2room.ironhide.exception.IronhideException;
import com.link2room.ironhide.facade.IHotelManagerFacadeService;
import com.link2room.ironhide.service.IHotelBaseService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by sss on 2016/9/18.
 */
@Component("hotelManagerFacadeService")
public class HotelManagerFacadeServiceImpl implements IHotelManagerFacadeService {
    @Resource
    private ICommonService commonService;

    @Resource
    private IHotelBaseService hotelBaseService;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 获得酒店列表
     * @param hotelsManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<HotelInfoDto> getHotelBaseInfoList(HotelsManagerParamDto hotelsManagerParamDto) throws Exception {
        L2RCondition l2RCondition1=new L2RCondition();
        l2RCondition1.setAndConditions(new ConditionDetail[]{
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ,hotelsManagerParamDto.getLhotelGroupCode()),
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelCode.toString(), CriteriaUtil.Operator.EQ,hotelsManagerParamDto.getLhotelCode()),
                new ConditionDetail(L2RHotelBaseinfo.L2RHotelBaseinfo_.city.toString(), CriteriaUtil.Operator.EQ,hotelsManagerParamDto.getCity()),
                new ConditionDetail(L2RHotelBaseinfo.L2RHotelBaseinfo_.status.toString(), CriteriaUtil.Operator.EQ, hotelsManagerParamDto.getStatus()),
                new ConditionDetail(BaseEntity.BaseEntity_.isHalt.toString(), CriteriaUtil.Operator.EQ, hotelsManagerParamDto.getIsHalt()),
                new ConditionDetail(L2RHotelBaseinfo.L2RHotelBaseinfo_.brand.toString(), CriteriaUtil.Operator.IN, hotelsManagerParamDto.getBrands()),
        });
        if(StringUtil.isNotBlank(hotelsManagerParamDto.getKey())){
            l2RCondition1.setOrConditions(new ConditionDetail[]{
                    new ConditionDetail(BaseEntity.BaseEntity_.lhotelCode.toString(), CriteriaUtil.Operator.LIKE,"%"+hotelsManagerParamDto.getKey()+"%"),
                    new ConditionDetail(L2RHotelBaseinfo.L2RHotelBaseinfo_.name.toString(), CriteriaUtil.Operator.LIKE,"%"+hotelsManagerParamDto.getKey()+"%"),
                    new ConditionDetail(L2RHotelBaseinfo.L2RHotelBaseinfo_.usedName.toString(), CriteriaUtil.Operator.LIKE, "%"+hotelsManagerParamDto.getKey()+"%")
            });
        }
        QueryResult<L2RHotelBaseinfo> hotelBaseInfoResult = commonService.findAllByCondition(L2RHotelBaseinfo.class, l2RCondition1,hotelsManagerParamDto.getCurrentPage(),hotelsManagerParamDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(hotelBaseInfoResult,HotelInfoDto.class, false);
    }

    /**
     * 保存或更新酒店明细
     * @param hotelsManagerParamDto
     * @throws Exception
     */
    @Override
    public void saveOrUpdateHotelBaseInfoDetail(HotelsManagerParamDto hotelsManagerParamDto)throws Exception{
        L2RHotelBaseinfo l2rHotelBaseInfo =ClassConverUtil.copyProperties(hotelsManagerParamDto.getHotelInfoDto(),L2RHotelBaseinfo.class);
        l2rHotelBaseInfo.setPositionType("GCJ-02");
        L2RHotelPicture bigPic = ClassConverUtil.copyProperties(hotelsManagerParamDto.getHotelInfoDto().getMainHotelPic(), L2RHotelPicture.class);
        List<L2RHotelPicture> hotelPics = ClassConverUtil.copyPropertiesList(hotelsManagerParamDto.getHotelInfoDto().getHotelPics(), L2RHotelPicture.class);
        hotelBaseService.saveOrUpdateHotelInfo(l2rHotelBaseInfo,bigPic,hotelPics);
        redisUtil.set(CommonUtil.getRedisKey(BaseConstant.RedisConstant.TITLE.HOTEL,l2rHotelBaseInfo.getLhotelCode()), ClassConverUtil.copyPropertiesToDto(l2rHotelBaseInfo, HotelBaseInfoDto.class, false));
    }

    /**
     * 保存或更新酒店
     * @param hotelsManagerParamDto
     * @throws Exception
     */
    @Override
    public void saveOrUpdateHotelBaseInfo(HotelsManagerParamDto hotelsManagerParamDto)throws Exception{
        L2RHotelBaseinfo l2rHotelBaseInfo =ClassConverUtil.copyProperties(hotelsManagerParamDto.getHotelInfoDto(),L2RHotelBaseinfo.class);
        l2rHotelBaseInfo.setPositionType("GCJ-02");
        commonService.saveOrUpdate(l2rHotelBaseInfo);
        redisUtil.set(CommonUtil.getRedisKey(BaseConstant.RedisConstant.TITLE.HOTEL,l2rHotelBaseInfo.getLhotelCode()), ClassConverUtil.copyPropertiesToDto(l2rHotelBaseInfo, HotelBaseInfoDto.class, false));
    }

    /**
     * 获得酒店基础详情
     * @param hotelsManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public HotelInfoDto getHotelBaseInfo(HotelsManagerParamDto hotelsManagerParamDto)throws Exception{
        L2RHotelBaseinfo l2rHotelBaseInfo = commonService.findOneById(L2RHotelBaseinfo.class, hotelsManagerParamDto.getId());
        HotelInfoDto hotelInfoDto=null;
        if(l2rHotelBaseInfo!=null){
            hotelInfoDto=ClassConverUtil.copyPropertiesToDto(l2rHotelBaseInfo,HotelInfoDto.class, false);
            L2RHotelPicture queryHPIC=new L2RHotelPicture();
            queryHPIC.setLhotelGroupCode(l2rHotelBaseInfo.getLhotelGroupCode());
            queryHPIC.setLhotelCode(l2rHotelBaseInfo.getLhotelCode());
            queryHPIC.setCategroy(BaseConstant.HotelBaseInfoConstant.PICCATEGROY.BIGPIC);
            QueryResult<L2RHotelPicture> bigPicResult = commonService.findAllEQ(L2RHotelPicture.class, queryHPIC);
            if(bigPicResult.getResultlist()!=null&&bigPicResult.getResultlist().size()>0){
                hotelInfoDto.setMainHotelPic(ClassConverUtil.copyPropertiesToDto(bigPicResult.getResultlist().get(0),L2RHotelPictureDto.class,false));
                hotelInfoDto.getMainHotelPic().setId(null);
            }
            queryHPIC.setCategroy(BaseConstant.HotelBaseInfoConstant.PICCATEGROY.PICS);
            QueryResult<L2RHotelPicture> picsResult = commonService.findAllEQ(L2RHotelPicture.class, queryHPIC);
            if(picsResult.getResultlist()!=null&&picsResult.getResultlist().size()>0) {
                hotelInfoDto.setHotelPics(ClassConverUtil.copyPropertiesListToDtoList(picsResult.getResultlist(), L2RHotelPictureDto.class, false));
                for(L2RHotelPictureDto hotelPic:hotelInfoDto.getHotelPics()){
                    hotelPic.setId(null);
                }
            }
        }
        return hotelInfoDto;
    }


    /**
     * 校驗酒店代碼
     * @param hotelsManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public void checkHotelCode(HotelsManagerParamDto hotelsManagerParamDto)throws Exception{
        L2RHotelBaseinfo queryH=new L2RHotelBaseinfo();
        queryH.setLhotelGroupCode(hotelsManagerParamDto.getLhotelGroupCode());
        queryH.setLhotelCode(hotelsManagerParamDto.getLhotelCode());
        QueryResult<L2RHotelBaseinfo> l2RHotelBaseinfoQueryResult = commonService.findAllEQ(L2RHotelBaseinfo.class,queryH);
        if(l2RHotelBaseinfoQueryResult.getResultlist()!=null&&l2RHotelBaseinfoQueryResult.getResultlist().size()>0) {
            throw new IronhideException(IronhideSysExceptionEnum.HOTEL_CODE_EXIT);
        }
    }
}
